﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.DBUtility;

namespace StoreWeb.ajax
{
    public partial class memberManage : System.Web.UI.Page
    {
        //BLL.Role rolebll = new BLL.Role();
        BLL.member memberManagebll = new BLL.member();
        BLL.memberGrade dealersGradebll = new BLL.memberGrade();
        BLL.sizeTemplate sizeTemplatebll = new BLL.sizeTemplate();
        BLL.sizeTemplateDetail sizeTemplateDetailbll = new BLL.sizeTemplateDetail();
        Maticsoft.BLL.membersize membersizebll = new Maticsoft.BLL.membersize();
        public string sizetemplateDetailstr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
                int row = 0;
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")
                {
                    Response.Write((memberManagebll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "confirm")
                {

                    //Model.member ne = memberManagebll.GetModel(Convert.ToInt32(Request["id"]));
                    //ne.userstate = "已审核";
                    //ne.audituser = Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]);
                    //ne.audittime = DateTime.Now; ;
                    //row = memberManagebll.Update(ne) ? 2 : 0;
                    //Response.Write(row);
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "sizeTemplate")
                {

                    Model.sizeTemplate ne = new Model.sizeTemplate();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        ne = sizeTemplatebll.GetModel(Convert.ToInt32(Request["id"]));
                        ne.name = Request["nametitile"];
                        row = sizeTemplatebll.Update(ne) ? 2 : 0;
                    }
                    else
                    {
                        ne.name = Request["nametitile"];
                        row = sizeTemplatebll.Add(ne) > 0 ? 1 : 0;
                    }
                    if (row == 1)
                    {
                        Response.Write("1");
                    }
                    else if (row == 2)
                    {
                        Response.Write("2");
                    }
                    else
                    {
                        Response.Write("0");
                    }

                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "deletesizeTemplate") 
                {

                    Response.Write((sizeTemplatebll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "deletesizeTemplates")//批量删除
                {

                    foreach (var item in Request["ids"].Split(','))
                    {
                        sizeTemplatebll.Delete(Convert.ToInt32(item));
                    }

                    Response.Write("1");
                }
                else if (!string.IsNullOrEmpty(Request["action"].ToString()) && Request["action"].ToString() == "sizeTemplateDetail")
                {
                    DbHelperSQLP db2 = new DbHelperSQLP();
                    db2.ExecuteSql("delete from sizeTemplateDetail where sizeTemplateId=" + Request["id"].ToString() + "");
                    string[] names = Request["name"].ToString().Split(',');
                    string[] values = Request["value"].ToString().Split(',');
                    for (int i = 0; i < names.Length; i++)
                    {
                        if (names[i].Trim() != "0")//数量不为0的才是用户选购的  才增加数据
                        {
                            Model.sizeTemplateDetail p = new Model.sizeTemplateDetail();
                            p.name = names[i].Trim();
                            p.value = values[i].Trim();
                            p.sizeTemplateId = Convert.ToInt32(Request["id"].ToString());
                            sizeTemplateDetailbll.Add(p);
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "selectsizeTemplate")
                {

                    //Response.Write(dealersGradebll.GetModelList("grade='" + Request["sizetemplate"] + "'")[0].discountrate);

                    int counts = sizeTemplateDetailbll.GetModelList(string.Format("sizeTemplateId={0}", Request["sizetemplate"])).Count;
                    if (counts > 0)
                    {
                        List<Model.sizeTemplateDetail> sizeTemplateDetails = sizeTemplateDetailbll.GetModelList(string.Format("sizeTemplateId={0}", Request["sizetemplate"]));
                        for (int i = 0; i < sizeTemplateDetails.Count; i++)
                        {
                            sizetemplateDetailstr += "<tr class=\"body\">";
                            sizetemplateDetailstr += "<td><input class=\"name\" type=\"text\" value=\"" + sizeTemplateDetails[i].name + "\"   name=\"name\"></td>";
                            sizetemplateDetailstr += "<td>";
                            sizetemplateDetailstr += "<span class=\"button minus\"></span>";
                            sizetemplateDetailstr += "<input class=\"number\" type=\"text\" value=\"" + sizeTemplateDetails[i].value + "\"    name=\"value\">";
                            sizetemplateDetailstr += "<span class=\"button plus\"></span>";
                            sizetemplateDetailstr += "</td>";
                            sizetemplateDetailstr += "<td><a class=\"del\" href=\"javascript:;\">删除</a></td>";
                            sizetemplateDetailstr += "</tr>";
                        }
                    }
                    else
                    {
                        sizetemplateDetailstr = "";
                    }
                    Response.Write(sizetemplateDetailstr);

                }
                else if (!string.IsNullOrEmpty(Request["action"].ToString()) && Request["action"].ToString() == "sizeTemplateDetailByMember")
                {
                    DbHelperSQLP db2 = new DbHelperSQLP();
                    db2.ExecuteSql("delete from membersize where membercode=" + Request["id"].ToString() + "");
                    string[] names = Request["name"].ToString().Split(',');
                    string[] values = Request["value"].ToString().Split(',');
                    string size = Request["size"].ToString();
                    for (int i = 0; i < names.Length; i++)
                    {
                        if (names[i].Trim() != "0")//
                        {
                            Maticsoft.Model.membersize p = new Maticsoft.Model.membersize();
                            p.name = names[i].Trim();
                            p.value = values[i].Trim();
                            p.membercode = Request["id"].ToString();
                            p.size = size;
                            membersizebll.Add(p);
                        }
                    }
                }
                else
                {
                    Model.member ne = new Model.member();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        ne = memberManagebll.GetModel(Convert.ToInt32(Request["id"]));

                        ne.membergrade = Convert.ToInt32(Request["memberGrade"]);

                        ne.isvip = Request["isvip"] == "RadioButton1" ? "0" : "1";
                        

                        row = memberManagebll.Update(ne) ? 2 : 0;
                    }
                    else
                    {
                        //ne.s_province = Request["s_province"];
                        //ne.s_city = Request["s_city"];
                        //ne.s_county = Request["s_county"];
                        //ne.dealersid = "0";//0代表没有上级经销商 因为这里是总部新建的经销商 没有指定谁是它上级
                        //ne.username = Request["username"];
                        //ne.mobile = Request["mobile"];
                       
                        //ne.dealersgrade = Convert.ToInt32(Request["dealersgrade"]);

                        //ne.userstate = "待审核";
                        //ne.description = Request["description"];
                        //ne.createtime = DateTime.Now;
                        //row = memberManagebll.Add(ne) > 0 ? 1 : 0;
                    }
                    if (row == 1)
                    {
                        Response.Write("1");
                    }else if (row == 2)
                    {
                        Response.Write("2");
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
            //}
        }
    }
}